package GUI;

import javax.swing.*;
import javax.swing.text.html.HTMLDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import 数据库和网络.myDatebase;
import 试题和试卷.*;

public class test extends JFrame{


    static test demo;
    public static JPanel mainPanel;
    public static JButton nextButton, preButton;
    static int currentIndex;
    static int selectionQuestionNum, definitionQuestionNum, fillingBlankQuestionNum, subjectiveQuestionNum;
    static ArrayList<questionItem> questionsItem;
    static ArrayList<question> questionSet;
    static paper currentpaper;
    public static String[][] resultSet;
    public static answerSheet ansboard;
    scoreWindow jumpingScore;

    public test() {
        questionSet = currentpaper.questionSet;

        selectionQuestionNum = currentpaper.getSelectionNumber();
        definitionQuestionNum = currentpaper.getDefinitionNumber();
        fillingBlankQuestionNum = currentpaper.getFillingBlankNumber();
        subjectiveQuestionNum = currentpaper.getsubjectiveNumber();
        currentIndex = 1;


        questionsItem = new ArrayList<>();
        questionsItem.add(new questionItem());

        enterWindow.currentStudent.getSchoolID();
        for (int i = 1; i <= selectionQuestionNum; i++) {
            questionsItem.add(new selectionQuestionItem(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        for (int i = 1; i <= definitionQuestionNum; i++) {
            questionsItem.add(new definitionQuestionItem(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        for (int i = 1; i <= fillingBlankQuestionNum; i++) {
            questionsItem.add(new fillingblankQuestionItem(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        for (int i = 1; i <= subjectiveQuestionNum; i++) {
            questionsItem.add(new subjectiveQuestionItem(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        currentIndex = 1;

        nextButton = new JButton("下一题");
        nextButton.setBackground(Color.lightGray);
        nextButton.setBounds(380, 560, 80, 30);
        nextButton.addActionListener(new nextListener());

        preButton = new JButton("上一题");
        preButton.setBackground(Color.lightGray);
        preButton.setBounds(180, 560, 80, 30);
        preButton.addActionListener(new preListener());

        mainPanel = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        Font bigFont = new Font("sanserif", Font.BOLD, 24);
        this.setJMenuBar(menuBar);
        //menuBar.setSize(this.getWidth(),30);
        JMenu saveMenu = new JMenu("Save");
        JMenu displayMenu = new JMenu("Display");
        JMenu submitMenu = new JMenu("Submit");
        JMenu quitMenu = new JMenu("Quit");
        JMenuItem save = new JMenuItem("暂存");
        JMenuItem display = new JMenuItem("显示答题卡");
        JMenuItem submit = new JMenuItem("交卷");
        JMenuItem quit = new JMenuItem("退出");
        menuBar.add(saveMenu);
        menuBar.add(displayMenu);
        menuBar.add(submitMenu);
        menuBar.add(quitMenu);
        save.addActionListener(new saveListener());
        saveMenu.add(save);
        display.addActionListener(new displayListener());
        displayMenu.add(display);
        submit.addActionListener(new submitListener());
        submitMenu.add(submit);
        quit.addActionListener(new quitListener());
        quitMenu.add(quit);

        mainPanel.setLayout(null);
        this.setTitle("开始做题");
        this.setContentPane(mainPanel);
        this.setSize(630, 750);
        mainPanel.setSize(this.getSize());
        this.setLocation(313,20);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        questionsItem.get(1).display();
        mainPanel.add(nextButton);
    }

    public test(paper currentpaper) {
        //System.out.println("请输入一个数字");
        //Scanner scan = new Scanner(System.in);
        this.resultSet = new String[2][currentpaper.getQuestionNumber()];
        this.currentpaper = currentpaper;
        questionSet = this.currentpaper.questionSet;
        for(int i=0;i<currentpaper.getQuestionNumber();i++){
            resultSet[0][i]=questionSet.get(i).getTitle();
            resultSet[1][i]="";
        }

        myDatebase saveDatebase=new myDatebase("result_record");
        try {
                String sql=String.format("select * from result_record.%s where studentID=%s",
                        currentpaper.getName(),enterWindow.currentStudent.getSchoolID());
                saveDatebase.statement.execute(sql);
                saveDatebase.resultSet=saveDatebase.statement.executeQuery(sql);
                while(saveDatebase.resultSet.next()){
                    for(int i=1;i<=currentpaper.getQuestionNumber();i++){
                        resultSet[1][i-1]=saveDatebase.resultSet.getString(resultSet[0][i-1]+"_current_answer");
                    }

                }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            saveDatebase.close();
        }



        selectionQuestionNum = currentpaper.getSelectionNumber();
        definitionQuestionNum = currentpaper.getDefinitionNumber();
        fillingBlankQuestionNum = currentpaper.getFillingBlankNumber();
        subjectiveQuestionNum = currentpaper.getsubjectiveNumber();
        currentIndex = 1;


        questionsItem = new ArrayList<>();
        questionsItem.add(new questionItem());

        for (int i = 1; i <= selectionQuestionNum; i++) {
            questionsItem.add(new selectionQuestionItem(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        for (int i = 1; i <= definitionQuestionNum; i++) {
            questionsItem.add(new definitionQuestionItem(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        for (int i = 1; i <= fillingBlankQuestionNum; i++) {
            questionsItem.add(new fillingblankQuestionItem(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        for (int i = 1; i <= subjectiveQuestionNum; i++) {
            questionsItem.add(new subjectiveQuestionItem(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }


        currentIndex = 1;


        nextButton = new JButton("下一题");
        nextButton.setBackground(Color.lightGray);
        nextButton.setBounds(380, 560, 80, 30);
        nextButton.addActionListener(new nextListener());

        preButton = new JButton("上一题");
        preButton.setBackground(Color.lightGray);
        preButton.setBounds(180, 560, 80, 30);
        preButton.addActionListener(new preListener());

        mainPanel = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        Font bigFont = new Font("sanserif", Font.BOLD, 24);
        this.setJMenuBar(menuBar);
        //menuBar.setSize(this.getWidth(),30);
        JMenu saveMenu = new JMenu("Save");
        JMenu displayMenu = new JMenu("Display");
        JMenu submitMenu = new JMenu("Submit");
        JMenu quitMenu = new JMenu("Quit");
        JMenuItem save = new JMenuItem("暂存");
        JMenuItem display = new JMenuItem("显示答题卡");
        JMenuItem submit = new JMenuItem("交卷");
        JMenuItem quit = new JMenuItem("退出");
        menuBar.add(saveMenu);
        menuBar.add(displayMenu);
        menuBar.add(submitMenu);
        menuBar.add(quitMenu);
        save.addActionListener(new saveListener());
        saveMenu.add(save);
        display.addActionListener(new displayListener());
        displayMenu.add(display);
        submit.addActionListener(new submitListener());
        submitMenu.add(submit);
        quit.addActionListener(new quitListener());
        quitMenu.add(quit);

        mainPanel.setLayout(null);
        this.setTitle("开始做题");
        this.setContentPane(mainPanel);
        this.setSize(630, 750);
        mainPanel.setSize(this.getSize());
        this.setLocation(313,20);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

        /*addSelection((selectionQuestionItem) questionsItem.get(1));*/
        questionsItem.get(1).loadAnswer();
        questionsItem.get(1).display();
        if (questionSet.size() > 1)
            mainPanel.add(nextButton);

    }

    public test(int index) {
        //System.out.println("请输入一个数字");
        //Scanner scan = new Scanner(System.in);



        selectionQuestionNum = currentpaper.getSelectionNumber();
        definitionQuestionNum = currentpaper.getDefinitionNumber();
        fillingBlankQuestionNum = currentpaper.getFillingBlankNumber();
        subjectiveQuestionNum = currentpaper.getsubjectiveNumber();


        currentIndex = 1;



        nextButton = new JButton("下一题");
        nextButton.setBackground(Color.lightGray);
        nextButton.setBounds(380, 560, 80, 30);
        nextButton.addActionListener(new nextListener());

        preButton = new JButton("上一题");
        preButton.setBackground(Color.lightGray);
        preButton.setBounds(180, 560, 80, 30);
        preButton.addActionListener(new preListener());

        mainPanel = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        Font bigFont = new Font("sanserif", Font.BOLD, 24);
        this.setJMenuBar(menuBar);
        //menuBar.setSize(this.getWidth(),30);
        JMenu saveMenu = new JMenu("Save");
        JMenu displayMenu = new JMenu("Display");
        JMenu submitMenu = new JMenu("Submit");
        JMenu quitMenu = new JMenu("Quit");
        JMenuItem save = new JMenuItem("暂存");
        JMenuItem display = new JMenuItem("显示答题卡");
        JMenuItem submit = new JMenuItem("交卷");
        JMenuItem quit = new JMenuItem("退出");
        menuBar.add(saveMenu);
        menuBar.add(displayMenu);
        menuBar.add(submitMenu);
        menuBar.add(quitMenu);
        save.addActionListener(new saveListener());
        saveMenu.add(save);
        display.addActionListener(new displayListener());
        displayMenu.add(display);
        submit.addActionListener(new submitListener());
        submitMenu.add(submit);
        quit.addActionListener(new quitListener());

        quitMenu.add(quit);

        mainPanel.setLayout(null);
        this.setTitle("开始做题");
        this.setContentPane(mainPanel);
        this.setSize(630, 750);
        mainPanel.setSize(this.getSize());
        this.setLocation(313,20);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);


        mainPanel.add(nextButton);
        mainPanel.add(preButton);
        currentIndex = index;
        questionsItem.get(currentIndex).loadAnswer();
        questionsItem.get(currentIndex).display();

        if (index == 1) {
            mainPanel.remove(preButton);
        }
        if (index == currentpaper.getQuestionNumber()) {
            mainPanel.remove(nextButton);
        }
        mainPanel.updateUI();
    }

    public class backListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            test.this.dispose();
        }
    }

    public class saveListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            myDatebase saveDatebase=new myDatebase("result_record");
            String name=currentpaper.getName();
            try {
                for(int i=1;i<=currentpaper.getQuestionNumber();i++){
                String sql=String.format("update %s SET %s_current_answer='"+resultSet[1][i-1]
                        +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name,
                        resultSet[0][i-1]);
                saveDatebase.statement.execute(sql);

                }
            } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
                saveDatebase.close();
            }

        }
    }

    public class displayListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ansboard.setVisible(true);
        }
    }

    public class submitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            myDatebase saveDatebase=new myDatebase("result_record");
            String name=currentpaper.getName();

            int a,b,aScore,bScore;
            a=0;
            aScore=0;
            b=0;
            bScore=0;
            for(int i=0;i<selectionQuestionNum;i++){
                if(resultSet[1][i].equals(currentpaper.questionSet.get(i).getAnswer())){
                    a++;
                    aScore+=currentpaper.questionSet.get(i).getFullScore();
                }
            }

            for(int i=selectionQuestionNum;i<definitionQuestionNum+selectionQuestionNum;i++){
                if(resultSet[1][i].equals(currentpaper.questionSet.get(i).getAnswer())){
                    b++;
                    bScore+=currentpaper.questionSet.get(i).getFullScore();
                }
            }

            jumpingScore=new scoreWindow(a,b,selectionQuestionNum,definitionQuestionNum,aScore,bScore);
            jumpingScore.setVisible(true);

            try {
                for(int i=1;i<=currentpaper.getQuestionNumber();i++){
                    String sql=String.format("update %s SET %s_current_answer='"+resultSet[1][i-1]
                                    +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name,
                            resultSet[0][i-1]);
                    saveDatebase.statement.execute(sql);
                    sql=String.format("update %s SET remainTime=0 where studentID='"+
                            enterWindow.currentStudent.getSchoolID()+"'",name);
                    saveDatebase.statement.execute(sql);
                    sql=String.format("update %s SET %s_proper_answer='"+questionSet.get(i-1).getAnswer()
                                    +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name,
                            resultSet[0][i-1]);
                    saveDatebase.statement.execute(sql);
                }
                String sql=String.format("update %s SET selection_score='"+aScore
                        +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name);
                saveDatebase.statement.execute(sql);
                sql=String.format("update %s SET definition_score='"+bScore
                        +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name);
                saveDatebase.statement.execute(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                saveDatebase.close();
            }
            test.this.dispose();
            test.ansboard.dispose();
            answerSheet.thread.stop();


            mainPanel.updateUI();
        }
    }

    public class quitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            myDatebase saveDatebase=new myDatebase("result_record");
            String name=currentpaper.getName();



            try {
                for(int i=1;i<=currentpaper.getQuestionNumber();i++){
                    String sql=String.format("update %s SET %s_current_answer='"+resultSet[1][i-1]
                                    +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name,
                            resultSet[0][i-1]);
                    saveDatebase.statement.execute(sql);

                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                saveDatebase.close();
            }
            test.this.dispose();
            test.ansboard.dispose();

            answerSheet.thread.stop();

        }
    }

    public class preListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            mainPanel.add(nextButton);
            mainPanel.updateUI();
            questionsItem.get(currentIndex--).remove();
            questionsItem.get(currentIndex).display();
            questionsItem.get(currentIndex).loadAnswer();
            if (currentIndex == 1) {
                mainPanel.remove(preButton);
            }

        }
    }

    public class nextListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            mainPanel.add(preButton);
            mainPanel.updateUI();
            questionsItem.get(currentIndex++).remove();
            questionsItem.get(currentIndex).display();
            questionsItem.get(currentIndex).loadAnswer();
            if (currentIndex == questionSet.size()) {
                mainPanel.remove(nextButton);
            }
        }
    }



}

